using System.Collections.Generic;
using LitJson;
using Localize;
using UnityEngine;
using UnityEngine.Assertions;

namespace abilities.parameters
{
    /**参数名，它的名字是信息窗口，图标等等。
     * * 描述参数、图标等的名称的类。*/
    public class ParameterEntry
    {
        static Dictionary<string, ParameterEntry> nameToParameter = new();
        static Dictionary<int, ParameterEntry> idToParameter = new();
        public int Id { get; private set; }
        /**名字： 健康、伤害。。。*/
        public string name { get; private set; }
        /**显示的图标	*/
        public string icon { get; private set; }
        /*参数不受其基本值的限制。*/
        public bool unlimited { get; private set; }
        
        public string LocalizedName=>i18n._(name);

        //定义几个常用属性，用于快速访问
        public static ParameterEntry HEALTH { get; private set; }
        public static ParameterEntry DAMAGE { get; private set; }
        public static ParameterEntry RANGE  { get; private set; }
        public static ParameterEntry SPEED  { get; private set; }
        public static ParameterEntry EXPERIENCE  { get; private set; }

        public ParameterEntry(int id, string name, string icon, bool unlimited)
        {
            Id = id;
            this.name = name;
            this.icon = icon;
            this.unlimited = unlimited;

            AssignByName(this, name);
        }
        public bool FromJson(JsonData value)
        {
            Id = (int)value["id"];
            Assert.IsTrue(Id < 255, "Parameter has id more than 255, it will cause troubles in replays");

            name = (string)value["name"];
            icon = (string)value["icon"];
            

            unlimited = value.ContainsKey("unlimited") && (bool)value["unlimited"];

            AssignByName(this, name);

            return true;
        }


        public bool IsUnlimited()
        {
            return unlimited;
        }

        public static ParameterEntry GetByName(string name)
        {
            if (nameToParameter.TryGetValue(name, out var value))
                return value;
            return null;
        }

        public static ParameterEntry GetById(int id)
        {
            if (idToParameter.TryGetValue(id, out var parameter))
                return parameter;
            return null;
        }

        public void AssignByName(ParameterEntry parameterEntry, string name)
        {
            if (name == "HEALTH")
            {
                HEALTH = parameterEntry;
            }
            else if (name == "RANGE")
            {
                RANGE = parameterEntry;
            }
            else if (name == "SPEED")
            {
                SPEED = parameterEntry;
            }
            else if (name == "EXPERIENCE")
            {
                EXPERIENCE = parameterEntry;
            }
            else
            {
                Debug.LogWarning($"Unknown parameter name {name} ");
            }
//TODO测试。
            nameToParameter.Add(name, parameterEntry);
            idToParameter.Add(parameterEntry.Id, parameterEntry);
        }
    }
}